const serverAddress = '154.8.228.43:8080'
const baseUrl = `http://${serverAddress}/`

const getResourceUrl = path => 'http://' + serverAddress + path

// 请求返回时的Error
class RequestError extends Error {
    constructor(message, status) {
        super()
        this.message = message
        this.status = status
    }
}

export const handleResponse = resp => {
    let data = resp.data
    if (data.status === 200) {
        return data.data
    } else {
        throw new RequestError(resp.data.message, resp.data.status)
    }
}

// request封装
class Request {
    constructor(baseUrl) {
        this.baseUrl = baseUrl
    }
    get(url, query) {
        return new Promise((resolve, reject) => wx.request({
            url: `${this.baseUrl}${url}`,
            data: query,
            header: {},
            method: 'GET',
            success: function(res) {
                resolve(res)
            },
            fail: function(res) {
                reject(res)
            }
        }))
    }
    post(url, data) {
        return new Promise((resolve, reject) => wx.request({
            url: `${this.baseUrl}${url}`,
            data: data,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            success: function(res) {
                resolve(res)
            },
            fail: function(res) {
                reject(res)
            }
        }))
    }
    upload(url, filePath, name) {
        return new Promise((resolve, reject) => wx.uploadFile({
            url: `${this.baseUrl}${url}`,
            filePath,
            name,
            success: function(res) {
                if (res.statusCode === 200) {

                    res.data = JSON.parse(res.data)
                    resolve(res)
                } else {
                    reject(res)
                }
            },
            fail: function(res) {
                reject(res)
            }
        }))

    }
}

const server = new Request(baseUrl)

const API = {
    login(username, password) {
        return server.post('login', {
            username,
            password
        }).then(handleResponse)
    },
    // 搜索规章预案
    getPlanFilesByKeyword(keyword) {
        return server.get('planFile/search', {
            keyword
        }).then(handleResponse)
	},
	// 获取最新规章预案
	getLatestPlanFiles(count=6) {
		return server.get('planFile/lastest', {
			count
		}).then(handleResponse)
	},
    // 获取所有预警信息
    getAllEmergencies() {
        return server.get('emergency').then(handleResponse)
    },
    // 发布预警信息
    addEmergency(bundle) {
        return server.post('emergency', bundle).then(handleResponse)
    },
    // 搜索详情指南
    getGuidenceFilesByKeyword(keyword) {
        return server.get('guidenceFile/search', {
            keyword
        }).then(handleResponse)
    },

    // 上传图片
    uploadImage(filePath) {
		return server.upload('upload/blob', filePath, 'file').then(handleResponse)
    },
    // 上传音频
    uploadAudio(filePath) {
		console.log(filePath)
		return server.upload('upload/blob', filePath, 'file').then(handleResponse)
    },
    // 上传视频
    uploadVideo(filePath) {
        return server.upload('upload/blob', filePath, 'file').then(handleResponse)
    },
    getResourceUrl
}

module.exports = API